/**
 * 
 * <p>
 * MSegInit
 * </p>
 * 
 * @author <a href="mailto:sylvain.meignier@lium.univ-lemans.fr">Sylvain Meignier</a>
 * @author <a href="mailto:gael.salaun@univ-lemans.fr">Gael Salaun</a>
 * @author <a href="mailto:teva.merlin@lium.univ-lemans.fr">Teva Merlin</a>
 * @version v2.0
 * 
 *          Copyright (c) 2007-2009 Universite du Maine. All Rights Reserved. Use is subject to license terms.
 * 
 *          THIS SOFTWARE IS PROVIDED BY THE "UNIVERSITE DU MAINE" AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 *          TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE
 *          LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *          GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 *          STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 *          OF SUCH DAMAGE.
 * 
 *          Initial segmentation program
 * 
 */

package fr.lium.spkDiarization.programs;

import java.io.IOException;
import java.util.Iterator;

import fr.lium.spkDiarization.lib.MainTools;
import fr.lium.spkDiarization.lib.DiarizationException;
import fr.lium.spkDiarization.libClusteringData.Cluster;
import fr.lium.spkDiarization.libClusteringData.ClusterSet;
import fr.lium.spkDiarization.libClusteringData.Segment;
import fr.lium.spkDiarization.libFeature.FeatureSet;
import fr.lium.spkDiarization.parameter.Parameter;

public class MSegInit {

	public static void make(FeatureSet features, ClusterSet clusters, ClusterSet clustersResult, Parameter param) throws DiarizationException, IOException {

		String initName = new String("S0");
		Cluster resCluster = clustersResult.createANewCluster(initName);

		// TreeSet<Segment> c = new TreeSet<Segment>();

		Iterator<Cluster> it = clusters.clusterSetValueIterator();

		while (it.hasNext()) {
			Cluster cluster = it.next();
			Iterator<Segment> itSeg = cluster.iterator();
			while (itSeg.hasNext()) {
				Segment seg = itSeg.next();

				features.setFileToDo(features.getCurrentShowName());
				int s = seg.getStart();
				int eSeg = s + seg.getLength();
				int e = Math.min(eSeg, features.getNumberOfFrames());
				if (param.trace) {
					System.out.println("trace[mSegInit] \t check segment : " + s + " " + e);
				}
				if (eSeg > e) {
					System.out.println("WARNING[mSegInit] \t segment end after features end");
				}
				boolean equal = false;
				for (int i = s + 1; i < e; i++) {
					if (features.compareFrames(i - 1, i)) {
						System.out.println("WARNING[mSegInit] \t two consecutive features are the same, index = " + i);
						equal = true;
					} else {
						if (equal == true) {
							equal = false;
						} else {
							Segment oneFrameSegment;
							if (i == (s + 1)) {
								oneFrameSegment = new Segment(seg.getShowName(), i - 1, 1, resCluster);
								resCluster.addSegment(oneFrameSegment);
							}
							oneFrameSegment = new Segment(seg.getShowName(), i, 1, resCluster);
							resCluster.addSegment(oneFrameSegment);
						}
					}

				}
			}
		}
	}

	public static void main(String[] args) throws DiarizationException, Exception {
		try {
			Parameter param = MainTools.getParameters("MSegInit", args);
			info(param, "MSegInit");
			if (param.nbShow > 0) {
				// clusters
				ClusterSet clusters = MainTools.readClusterSet(param);

				// Features
				FeatureSet features = MainTools.readFeatureSet(param, clusters);

				// clusters result
				// TODO : check this
				ClusterSet res = (ClusterSet) (clusters.clone());
				res.clearClusters();

				make(features, clusters, res, param);

				MainTools.writeClusterSet(param, res, true);
			}
		} catch (DiarizationException e) {
			System.out.println("error \t exception " + e.getMessage());
		}

	}

	public static void info(Parameter param, String prog) {
		if (param.help) {
			param.printSeparator2();
			System.out.println("info[program] \t name = " + prog);
			param.printSeparator();
			param.printShow();

			param.parameterInputFeature.printMask(); // fInMask
			param.parameterInputFeature.printDescription(); // fDesc
			param.printSeparator();
			param.parameterSegmentationInputFile.printMask(); // sInMask
			param.parameterSegmentationInputFile.printEncodingFormat();
			param.parameterSegmentationOutputFile.printMask(); // sOutMask
			param.parameterSegmentationOutputFile.printEncodingFormat();
			param.printSeparator();
		}
	}

}